home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectSound / Play3DSound / Play3DSound.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  42.0 KB  |  951 lines

  1. //----------------------------------------------------------------------------
  2. // File: Play3DSound.cs
  3. //
  4. // Copyright (c) Microsoft Corp. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using System.Drawing;
  8. using Color = System.Drawing.Color;
  9. using System.Windows.Forms;
  10. using Microsoft.DirectX;
  11. using Microsoft.DirectX.DirectSound;
  12. using Microsoft.Samples.DirectX.UtilityToolkit;
  13. using Buffer = Microsoft.DirectX.DirectSound.Buffer;
  14.  
  15. public class Play3DSound : Form
  16. {
  17.     #region Class Variables
  18.     private System.Windows.Forms.Button buttonSoundfile;
  19.     private Label labelFilename;
  20.     private Label labelStatic;
  21.     private Label labelStatus;
  22.     private GroupBox groupboxStatic;
  23.     private Label labelStatic1;
  24.     private Label labelDopplerfactor;
  25.     private TrackBar trackbarDopplerSlider;
  26.     private Label labelStatic2;
  27.     private Label labelRollofffactor;
  28.     private TrackBar trackbarRolloffSlider;
  29.     private Label labelStatic3;
  30.     private Label labelMindistance;
  31.     private TrackBar trackbarMindistanceSlider;
  32.     private Label labelStatic4;
  33.     private Label labelMaxdistance;
  34.     private TrackBar trackbarMaxdistanceSlider;
  35.     private GroupBox groupboxStatic1;
  36.     private PictureBox pictureboxRenderWindow;
  37.     private TrackBar trackbarVerticalSlider;
  38.     private TrackBar trackbarHorizontalSlider;
  39.     private System.Windows.Forms.Button buttonPlay;
  40.     private System.Windows.Forms.Button buttonStop;
  41.     private System.Windows.Forms.Button buttonCancel;
  42.     private CheckBox checkboxDefer;
  43.     private System.Windows.Forms.Button buttonApply;
  44.     private System.Timers.Timer timerMovement;
  45.     #endregion
  46.     
  47.     private Listener3DSettings listenerParameters = new Listener3DSettings();
  48.     private Buffer3DSettings application3DSettings = new Buffer3DSettings();
  49.     private SecondaryBuffer applicationBuffer = null;
  50.     public static Guid guid3DAlgorithm = Guid.Empty;
  51.     private Device applicationDevice = null;
  52.     private Listener3D applicationListener = null;
  53.     private Buffer3D applicationBuffer3D = null;
  54.     private Graphics applicationGraphics = null;
  55.     private const float maxOrbitRadius = 1.0f;
  56.     private string FileName = string.Empty;
  57.     private string Path = string.Empty;
  58.     private Bitmap bitmapGrid = null;
  59.     private int GridHeight = 0;
  60.     private int GridWidth = 0;
  61.     private int X = 0;
  62.     private int Y = 0;
  63.  
  64.     public static void Main()
  65.     {
  66.         try
  67.         {
  68.             using (Play3DSound form = new Play3DSound())
  69.             {
  70.                 Application.Run(form);
  71.             }
  72.         }
  73.         catch{}
  74.     }
  75.     
  76.     public Play3DSound()
  77.     {
  78.         try
  79.         {
  80.             // Load the icon from our resources
  81.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(this.GetType());
  82.             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  83.         }
  84.         catch
  85.         {
  86.             // It's no big deal if we can't load our icons, but try to load the embedded one
  87.             try { this.Icon = new System.Drawing.Icon(this.GetType(), "directx.ico"); } 
  88.             catch {}
  89.         }
  90.         //
  91.         // Required for Windows Form Designer support
  92.         //
  93.  
  94.         InitializeComponent();
  95.  
  96.         BufferDescription description = new BufferDescription();
  97.         WaveFormat fmt = new WaveFormat();
  98.         description.PrimaryBuffer = true;
  99.         description.Control3D = true;
  100.         Buffer buff = null;
  101.         
  102.         fmt.FormatTag = WaveFormatTag.Pcm;
  103.         fmt.Channels = 2;
  104.         fmt.SamplesPerSecond = 22050;
  105.         fmt.BitsPerSample = 16;
  106.         fmt.BlockAlign = (short)(fmt.BitsPerSample / 8 * fmt.Channels);
  107.         fmt.AverageBytesPerSecond = fmt.SamplesPerSecond * fmt.BlockAlign;
  108.  
  109.         try
  110.         {
  111.             applicationDevice = new Device();
  112.             applicationDevice.SetCooperativeLevel(this, CooperativeLevel.Priority);
  113.         }
  114.         catch
  115.         {
  116.             MessageBox.Show("Unable to create sound device. Sample will now exit.");
  117.             this.Close();
  118.             throw;
  119.         }
  120.         
  121.         // Get the primary buffer and set the format.
  122.         buff = new Buffer(description, applicationDevice);
  123.         buff.Format = fmt;
  124.  
  125.         applicationListener = new Listener3D(buff);
  126.         listenerParameters = applicationListener.AllParameters;
  127.  
  128.         labelFilename.Text = String.Empty;
  129.         labelStatus.Text = "No file loaded.";
  130.  
  131.         string path = Utility.FindMediaFile("grid.jpg");
  132.         pictureboxRenderWindow.BackgroundImage = Image.FromFile(path);
  133.         GridWidth = pictureboxRenderWindow.Width;
  134.         GridHeight = pictureboxRenderWindow.Height;
  135.  
  136.         trackbarDopplerSlider.Maximum = ConvertLogScaleToLinearSliderPos(DSoundHelper.MaxDopplerFactor);
  137.         trackbarDopplerSlider.Minimum = ConvertLogScaleToLinearSliderPos(DSoundHelper.MinDopplerFactor);
  138.  
  139.         trackbarRolloffSlider.Maximum = ConvertLogScaleToLinearSliderPos(DSoundHelper.MaxRolloffFactor);
  140.         trackbarRolloffSlider.Minimum = ConvertLogScaleToLinearSliderPos(DSoundHelper.MinRolloffFactor);
  141.  
  142.         trackbarMindistanceSlider.Maximum = 40;
  143.         trackbarMindistanceSlider.Minimum = 1;
  144.  
  145.         trackbarMaxdistanceSlider.Maximum = 40;
  146.         trackbarMaxdistanceSlider.Minimum = 1;
  147.  
  148.         trackbarVerticalSlider.Maximum = 100;
  149.         trackbarVerticalSlider.Minimum = -100;
  150.         trackbarVerticalSlider.Value = 100;
  151.  
  152.         trackbarHorizontalSlider.Maximum = 100;
  153.         trackbarHorizontalSlider.Minimum = -100;
  154.         trackbarHorizontalSlider.Value = 100;
  155.  
  156.         SetSlidersPos(0, 0, maxOrbitRadius, maxOrbitRadius * 20.0f);
  157.         SliderChanged();
  158.     }
  159.     #region InitializeComponent code
  160.     private void InitializeComponent()
  161.     {
  162.         this.buttonSoundfile = new System.Windows.Forms.Button();
  163.         this.labelFilename = new System.Windows.Forms.Label();
  164.         this.labelStatic = new System.Windows.Forms.Label();
  165.         this.labelStatus = new System.Windows.Forms.Label();
  166.         this.groupboxStatic = new System.Windows.Forms.GroupBox();
  167.         this.buttonApply = new System.Windows.Forms.Button();
  168.         this.trackbarMaxdistanceSlider = new System.Windows.Forms.TrackBar();
  169.         this.trackbarDopplerSlider = new System.Windows.Forms.TrackBar();
  170.         this.trackbarMindistanceSlider = new System.Windows.Forms.TrackBar();
  171.         this.trackbarRolloffSlider = new System.Windows.Forms.TrackBar();
  172.         this.checkboxDefer = new System.Windows.Forms.CheckBox();
  173.         this.labelMaxdistance = new System.Windows.Forms.Label();
  174.         this.labelStatic4 = new System.Windows.Forms.Label();
  175.         this.labelRollofffactor = new System.Windows.Forms.Label();
  176.         this.labelStatic2 = new System.Windows.Forms.Label();
  177.         this.labelStatic3 = new System.Windows.Forms.Label();
  178.         this.labelMindistance = new System.Windows.Forms.Label();
  179.         this.labelStatic1 = new System.Windows.Forms.Label();
  180.         this.labelDopplerfactor = new System.Windows.Forms.Label();
  181.         this.groupboxStatic1 = new System.Windows.Forms.GroupBox();
  182.         this.pictureboxRenderWindow = new System.Windows.Forms.PictureBox();
  183.         this.trackbarVerticalSlider = new System.Windows.Forms.TrackBar();
  184.         this.trackbarHorizontalSlider = new System.Windows.Forms.TrackBar();
  185.         this.buttonPlay = new System.Windows.Forms.Button();
  186.         this.buttonStop = new System.Windows.Forms.Button();
  187.         this.buttonCancel = new System.Windows.Forms.Button();
  188.         this.timerMovement = new System.Timers.Timer();
  189.         this.groupboxStatic.SuspendLayout();
  190.         ((System.ComponentModel.ISupportInitialize)(this.trackbarMaxdistanceSlider)).BeginInit();
  191.         ((System.ComponentModel.ISupportInitialize)(this.trackbarDopplerSlider)).BeginInit();
  192.         ((System.ComponentModel.ISupportInitialize)(this.trackbarMindistanceSlider)).BeginInit();
  193.         ((System.ComponentModel.ISupportInitialize)(this.trackbarRolloffSlider)).BeginInit();
  194.         ((System.ComponentModel.ISupportInitialize)(this.trackbarVerticalSlider)).BeginInit();
  195.         ((System.ComponentModel.ISupportInitialize)(this.trackbarHorizontalSlider)).BeginInit();
  196.         ((System.ComponentModel.ISupportInitialize)(this.timerMovement)).BeginInit();
  197.         this.SuspendLayout();
  198.         // 
  199.         // buttonSoundfile
  200.         // 
  201.         this.buttonSoundfile.Location = new System.Drawing.Point(10, 11);
  202.         this.buttonSoundfile.Name = "buttonSoundfile";
  203.         this.buttonSoundfile.Size = new System.Drawing.Size(78, 21);
  204.         this.buttonSoundfile.TabIndex = 0;
  205.         this.buttonSoundfile.Text = "Sound &file...";
  206.         this.buttonSoundfile.Click += new System.EventHandler(this.buttonSoundfile_Click);
  207.         // 
  208.         // labelFilename
  209.         // 
  210.         this.labelFilename.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  211.         this.labelFilename.Location = new System.Drawing.Point(102, 11);
  212.         this.labelFilename.Name = "labelFilename";
  213.         this.labelFilename.Size = new System.Drawing.Size(334, 21);
  214.         this.labelFilename.TabIndex = 1;
  215.         this.labelFilename.Text = "Static";
  216.         this.labelFilename.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  217.         // 
  218.         // labelStatic
  219.         // 
  220.         this.labelStatic.Location = new System.Drawing.Point(10, 41);
  221.         this.labelStatic.Name = "labelStatic";
  222.         this.labelStatic.Size = new System.Drawing.Size(63, 21);
  223.         this.labelStatic.TabIndex = 2;
  224.         this.labelStatic.Text = "Status";
  225.         this.labelStatic.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  226.         // 
  227.         // labelStatus
  228.         // 
  229.         this.labelStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  230.         this.labelStatus.Location = new System.Drawing.Point(102, 41);
  231.         this.labelStatus.Name = "labelStatus";
  232.         this.labelStatus.Size = new System.Drawing.Size(334, 21);
  233.         this.labelStatus.TabIndex = 3;
  234.         this.labelStatus.Text = "Static";
  235.         this.labelStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  236.         // 
  237.         // groupboxStatic
  238.         // 
  239.         this.groupboxStatic.Controls.AddRange(new System.Windows.Forms.Control[] {
  240.                                                                                this.buttonApply,
  241.                                                                                this.trackbarMaxdistanceSlider,
  242.                                                                                this.trackbarDopplerSlider,
  243.                                                                                this.trackbarMindistanceSlider,
  244.                                                                                this.trackbarRolloffSlider,
  245.                                                                                this.checkboxDefer,
  246.                                                                                this.labelMaxdistance,
  247.                                                                                this.labelStatic4,
  248.                                                                                this.labelRollofffactor,
  249.                                                                                this.labelStatic2,
  250.                                                                                this.labelStatic3,
  251.                                                                                this.labelMindistance});
  252.         this.groupboxStatic.Location = new System.Drawing.Point(10, 72);
  253.         this.groupboxStatic.Name = "groupboxStatic";
  254.         this.groupboxStatic.Size = new System.Drawing.Size(300, 240);
  255.         this.groupboxStatic.TabIndex = 4;
  256.         this.groupboxStatic.TabStop = false;
  257.         this.groupboxStatic.Text = "Sound properties";
  258.         // 
  259.         // buttonApply
  260.         // 
  261.         this.buttonApply.Location = new System.Drawing.Point(184, 208);
  262.         this.buttonApply.Name = "buttonApply";
  263.         this.buttonApply.Size = new System.Drawing.Size(88, 18);
  264.         this.buttonApply.TabIndex = 25;
  265.         this.buttonApply.Text = "Apply Settings";
  266.         this.buttonApply.Click += new System.EventHandler(this.buttonApply_Click);
  267.         // 
  268.         // trackbarMaxdistanceSlider
  269.         // 
  270.         this.trackbarMaxdistanceSlider.AutoSize = false;
  271.         this.trackbarMaxdistanceSlider.Location = new System.Drawing.Point(160, 168);
  272.         this.trackbarMaxdistanceSlider.Name = "trackbarMaxdistanceSlider";
  273.         this.trackbarMaxdistanceSlider.Size = new System.Drawing.Size(135, 45);
  274.         this.trackbarMaxdistanceSlider.TabIndex = 16;
  275.         this.trackbarMaxdistanceSlider.Text = "Slider3";
  276.         this.trackbarMaxdistanceSlider.TickStyle = System.Windows.Forms.TickStyle.None;
  277.         this.trackbarMaxdistanceSlider.Scroll += new System.EventHandler(this.trackbarSlider_Scroll);
  278.         // 
  279.         // trackbarDopplerSlider
  280.         // 
  281.         this.trackbarDopplerSlider.AutoSize = false;
  282.         this.trackbarDopplerSlider.Location = new System.Drawing.Point(160, 24);
  283.         this.trackbarDopplerSlider.Name = "trackbarDopplerSlider";
  284.         this.trackbarDopplerSlider.Size = new System.Drawing.Size(135, 45);
  285.         this.trackbarDopplerSlider.TabIndex = 7;
  286.         this.trackbarDopplerSlider.Text = "Slider1";
  287.         this.trackbarDopplerSlider.TickStyle = System.Windows.Forms.TickStyle.None;
  288.         this.trackbarDopplerSlider.Scroll += new System.EventHandler(this.trackbarSlider_Scroll);
  289.         // 
  290.         // trackbarMindistanceSlider
  291.         // 
  292.         this.trackbarMindistanceSlider.AutoSize = false;
  293.         this.trackbarMindistanceSlider.Location = new System.Drawing.Point(160, 120);
  294.         this.trackbarMindistanceSlider.Name = "trackbarMindistanceSlider";
  295.         this.trackbarMindistanceSlider.Size = new System.Drawing.Size(135, 45);
  296.         this.trackbarMindistanceSlider.TabIndex = 13;
  297.         this.trackbarMindistanceSlider.Text = "Slider3";
  298.         this.trackbarMindistanceSlider.TickStyle = System.Windows.Forms.TickStyle.None;
  299.         this.trackbarMindistanceSlider.Scroll += new System.EventHandler(this.trackbarSlider_Scroll);
  300.         // 
  301.         // trackbarRolloffSlider
  302.         // 
  303.         this.trackbarRolloffSlider.AutoSize = false;
  304.         this.trackbarRolloffSlider.Location = new System.Drawing.Point(160, 72);
  305.         this.trackbarRolloffSlider.Name = "trackbarRolloffSlider";
  306.         this.trackbarRolloffSlider.Size = new System.Drawing.Size(135, 45);
  307.         this.trackbarRolloffSlider.TabIndex = 10;
  308.         this.trackbarRolloffSlider.Text = "Slider2";
  309.         this.trackbarRolloffSlider.TickStyle = System.Windows.Forms.TickStyle.None;
  310.         this.trackbarRolloffSlider.Scroll += new System.EventHandler(this.trackbarSlider_Scroll);
  311.         // 
  312.         // checkboxDefer
  313.         // 
  314.         this.checkboxDefer.Appearance = System.Windows.Forms.Appearance.Button;
  315.         this.checkboxDefer.FlatStyle = System.Windows.Forms.FlatStyle.System;
  316.         this.checkboxDefer.Location = new System.Drawing.Point(64, 208);
  317.         this.checkboxDefer.Name = "checkboxDefer";
  318.         this.checkboxDefer.Size = new System.Drawing.Size(87, 18);
  319.         this.checkboxDefer.TabIndex = 24;
  320.         this.checkboxDefer.Text = "Defer Settings";
  321.         this.checkboxDefer.CheckedChanged += new System.EventHandler(this.checkboxDefer_CheckedChanged);
  322.         // 
  323.         // labelMaxdistance
  324.         // 
  325.         this.labelMaxdistance.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  326.         this.labelMaxdistance.Location = new System.Drawing.Point(96, 168);
  327.         this.labelMaxdistance.Name = "labelMaxdistance";
  328.         this.labelMaxdistance.Size = new System.Drawing.Size(52, 21);
  329.         this.labelMaxdistance.TabIndex = 15;
  330.         this.labelMaxdistance.Text = "0";
  331.         this.labelMaxdistance.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  332.         // 
  333.         // labelStatic4
  334.         // 
  335.         this.labelStatic4.Location = new System.Drawing.Point(16, 168);
  336.         this.labelStatic4.Name = "labelStatic4";
  337.         this.labelStatic4.Size = new System.Drawing.Size(71, 21);
  338.         this.labelStatic4.TabIndex = 14;
  339.         this.labelStatic4.Text = "Max distance";
  340.         this.labelStatic4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  341.         // 
  342.         // labelRollofffactor
  343.         // 
  344.         this.labelRollofffactor.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  345.         this.labelRollofffactor.Location = new System.Drawing.Point(96, 72);
  346.         this.labelRollofffactor.Name = "labelRollofffactor";
  347.         this.labelRollofffactor.Size = new System.Drawing.Size(52, 21);
  348.         this.labelRollofffactor.TabIndex = 9;
  349.         this.labelRollofffactor.Text = "0";
  350.         this.labelRollofffactor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  351.         // 
  352.         // labelStatic2
  353.         // 
  354.         this.labelStatic2.Location = new System.Drawing.Point(16, 72);
  355.         this.labelStatic2.Name = "labelStatic2";
  356.         this.labelStatic2.Size = new System.Drawing.Size(71, 21);
  357.         this.labelStatic2.TabIndex = 8;
  358.         this.labelStatic2.Text = "Rolloff factor";
  359.         this.labelStatic2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  360.         // 
  361.         // labelStatic3
  362.         // 
  363.         this.labelStatic3.Location = new System.Drawing.Point(16, 120);
  364.         this.labelStatic3.Name = "labelStatic3";
  365.         this.labelStatic3.Size = new System.Drawing.Size(71, 21);
  366.         this.labelStatic3.TabIndex = 11;
  367.         this.labelStatic3.Text = "Min distance";
  368.         this.labelStatic3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  369.         // 
  370.         // labelMindistance
  371.         // 
  372.         this.labelMindistance.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  373.         this.labelMindistance.Location = new System.Drawing.Point(96, 120);
  374.         this.labelMindistance.Name = "labelMindistance";
  375.         this.labelMindistance.Size = new System.Drawing.Size(52, 21);
  376.         this.labelMindistance.TabIndex = 12;
  377.         this.labelMindistance.Text = "0";
  378.         this.labelMindistance.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  379.         // 
  380.         // labelStatic1
  381.         // 
  382.         this.labelStatic1.Location = new System.Drawing.Point(25, 96);
  383.         this.labelStatic1.Name = "labelStatic1";
  384.         this.labelStatic1.Size = new System.Drawing.Size(75, 21);
  385.         this.labelStatic1.TabIndex = 5;
  386.         this.labelStatic1.Text = "Doppler factor";
  387.         this.labelStatic1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  388.         // 
  389.         // labelDopplerfactor
  390.         // 
  391.         this.labelDopplerfactor.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  392.         this.labelDopplerfactor.Location = new System.Drawing.Point(108, 96);
  393.         this.labelDopplerfactor.Name = "labelDopplerfactor";
  394.         this.labelDopplerfactor.Size = new System.Drawing.Size(52, 21);
  395.         this.labelDopplerfactor.TabIndex = 6;
  396.         this.labelDopplerfactor.Text = "0";
  397.         this.labelDopplerfactor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  398.         // 
  399.         // groupboxStatic1
  400.         // 
  401.         this.groupboxStatic1.Location = new System.Drawing.Point(318, 72);
  402.         this.groupboxStatic1.Name = "groupboxStatic1";
  403.         this.groupboxStatic1.Size = new System.Drawing.Size(162, 240);
  404.         this.groupboxStatic1.TabIndex = 17;
  405.         this.groupboxStatic1.TabStop = false;
  406.         this.groupboxStatic1.Text = "Sound movement";
  407.         // 
  408.         // pictureboxRenderWindow
  409.         // 
  410.         this.pictureboxRenderWindow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  411.         this.pictureboxRenderWindow.Location = new System.Drawing.Point(352, 136);
  412.         this.pictureboxRenderWindow.Name = "pictureboxRenderWindow";
  413.         this.pictureboxRenderWindow.Size = new System.Drawing.Size(69, 70);
  414.         this.pictureboxRenderWindow.TabIndex = 18;
  415.         this.pictureboxRenderWindow.TabStop = false;
  416.         this.pictureboxRenderWindow.Text = "130";
  417.         // 
  418.         // trackbarVerticalSlider
  419.         // 
  420.         this.trackbarVerticalSlider.AutoSize = false;
  421.         this.trackbarVerticalSlider.Location = new System.Drawing.Point(432, 128);
  422.         this.trackbarVerticalSlider.Name = "trackbarVerticalSlider";
  423.         this.trackbarVerticalSlider.Orientation = System.Windows.Forms.Orientation.Vertical;
  424.         this.trackbarVerticalSlider.Size = new System.Drawing.Size(45, 84);
  425.         this.trackbarVerticalSlider.TabIndex = 19;
  426.         this.trackbarVerticalSlider.Text = "Slider1";
  427.         this.trackbarVerticalSlider.TickStyle = System.Windows.Forms.TickStyle.None;
  428.         this.trackbarVerticalSlider.Scroll += new System.EventHandler(this.trackbarSlider_Scroll);
  429.         // 
  430.         // trackbarHorizontalSlider
  431.         // 
  432.         this.trackbarHorizontalSlider.AutoSize = false;
  433.         this.trackbarHorizontalSlider.Location = new System.Drawing.Point(344, 208);
  434.         this.trackbarHorizontalSlider.Name = "trackbarHorizontalSlider";
  435.         this.trackbarHorizontalSlider.Size = new System.Drawing.Size(82, 45);
  436.         this.trackbarHorizontalSlider.TabIndex = 20;
  437.         this.trackbarHorizontalSlider.Text = "Slider1";
  438.         this.trackbarHorizontalSlider.TickStyle = System.Windows.Forms.TickStyle.None;
  439.         this.trackbarHorizontalSlider.Scroll += new System.EventHandler(this.trackbarSlider_Scroll);
  440.         // 
  441.         // buttonPlay
  442.         // 
  443.         this.buttonPlay.Enabled = false;
  444.         this.buttonPlay.Location = new System.Drawing.Point(8, 328);
  445.         this.buttonPlay.Name = "buttonPlay";
  446.         this.buttonPlay.TabIndex = 21;
  447.         this.buttonPlay.Text = "&Play";
  448.         this.buttonPlay.Click += new System.EventHandler(this.buttonPlay_Click);
  449.         // 
  450.         // buttonStop
  451.         // 
  452.         this.buttonStop.Enabled = false;
  453.         this.buttonStop.Location = new System.Drawing.Point(88, 328);
  454.         this.buttonStop.Name = "buttonStop";
  455.         this.buttonStop.TabIndex = 22;
  456.         this.buttonStop.Text = "&Stop";
  457.         this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
  458.         // 
  459.         // buttonCancel
  460.         // 
  461.         this.buttonCancel.Location = new System.Drawing.Point(400, 328);
  462.         this.buttonCancel.Name = "buttonCancel";
  463.         this.buttonCancel.TabIndex = 23;
  464.         this.buttonCancel.Text = "E&xit";
  465.         this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
  466.         // 
  467.         // timerMovement
  468.         // 
  469.         this.timerMovement.Enabled = true;
  470.         this.timerMovement.SynchronizingObject = this;
  471.         this.timerMovement.Elapsed += new System.Timers.ElapsedEventHandler(this.MovementTimer_Elapsed);
  472.         // 
  473.         // Play3DSound
  474.         // 
  475.         this.AcceptButton = this.buttonSoundfile;
  476.         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  477.         this.ClientSize = new System.Drawing.Size(496, 358);
  478.         this.Controls.AddRange(new System.Windows.Forms.Control[] {
  479.                                                                       this.buttonSoundfile,
  480.                                                                       this.labelFilename,
  481.                                                                       this.labelStatic,
  482.                                                                       this.labelStatus,
  483.                                                                       this.labelStatic1,
  484.                                                                       this.labelDopplerfactor,
  485.                                                                       this.pictureboxRenderWindow,
  486.                                                                       this.trackbarVerticalSlider,
  487.                                                                       this.trackbarHorizontalSlider,
  488.                                                                       this.buttonPlay,
  489.                                                                       this.buttonStop,
  490.                                                                       this.buttonCancel,
  491.                                                                       this.groupboxStatic,
  492.                                                                       this.groupboxStatic1});
  493.         this.Name = "Play3DSound";
  494.         this.Text = "Play3DSound";
  495.         this.groupboxStatic.ResumeLayout(false);
  496.         ((System.ComponentModel.ISupportInitialize)(this.trackbarMaxdistanceSlider)).EndInit();
  497.         ((System.ComponentModel.ISupportInitialize)(this.trackbarDopplerSlider)).EndInit();
  498.         ((System.ComponentModel.ISupportInitialize)(this.trackbarMindistanceSlider)).EndInit();
  499.         ((System.ComponentModel.ISupportInitialize)(this.trackbarRolloffSlider)).EndInit();
  500.         ((System.ComponentModel.ISupportInitialize)(this.trackbarVerticalSlider)).EndInit();
  501.         ((System.ComponentModel.ISupportInitialize)(this.trackbarHorizontalSlider)).EndInit();
  502.         ((System.ComponentModel.ISupportInitialize)(this.timerMovement)).EndInit();
  503.         this.ResumeLayout(false);
  504.  
  505.     }
  506.     #endregion
  507.     int ConvertLogScaleToLinearSliderPos(float fValue)
  508.     {
  509.         //-----------------------------------------------------------------------------
  510.         // Name: ConvertLinearSliderPosToLogScale()
  511.         // Desc: Converts a quasi logrithmic scale to a slider position
  512.         //-----------------------------------------------------------------------------
  513.         if (fValue > 0.0f && fValue <= 0.1f)
  514.         {
  515.             return (int)(fValue/0.01f);
  516.         }
  517.         else if (fValue > 0.1f && fValue <= 1.0f)
  518.         {
  519.             return (int)(fValue/0.1f) + 10;
  520.         }
  521.         else if (fValue > 1.0f && fValue <= 10.0f)
  522.         {
  523.             return (int)(fValue/1.0f) + 20;
  524.         }
  525.         else if (fValue > 10.0f && fValue <= 100.0f)
  526.         {
  527.             return (int)(fValue/10.0f) + 30;
  528.         }
  529.         return 0;
  530.     }
  531.     float ConvertLinearSliderPosToLogScale(int iSliderPos)
  532.     {
  533.         if (iSliderPos > 0 && iSliderPos <= 10)
  534.         {
  535.             return iSliderPos*0.01f;
  536.         }
  537.         else if (iSliderPos > 10 && iSliderPos <= 20)
  538.         {
  539.             return (iSliderPos-10)*0.1f;
  540.         }
  541.         else if (iSliderPos > 20 && iSliderPos <= 30)
  542.         {
  543.             return (iSliderPos-20)*1.0f;
  544.         }
  545.         else if (iSliderPos > 30 && iSliderPos <= 40)
  546.         {
  547.             return (iSliderPos-30)*10.0f;
  548.         }
  549.         return 0.0f;
  550.     }
  551.     void SetSlidersPos(float fDopplerValue, float fRolloffValue, float fMinDistValue, float fMaxDistValue)
  552.     {
  553.         //-----------------------------------------------------------------------------
  554.         // Name: SetSlidersPos()
  555.         // Desc: Sets the slider positions
  556.         //-----------------------------------------------------------------------------
  557.         int lDopplerSlider = ConvertLogScaleToLinearSliderPos(fDopplerValue);
  558.         int lRolloffSlider = ConvertLogScaleToLinearSliderPos(fRolloffValue);
  559.         int lMinDistSlider = ConvertLogScaleToLinearSliderPos(fMinDistValue);
  560.         int lMaxDistSlider = ConvertLogScaleToLinearSliderPos(fMaxDistValue);
  561.  
  562.         trackbarDopplerSlider.Value = lDopplerSlider;
  563.         trackbarRolloffSlider.Value = lRolloffSlider;
  564.         trackbarMindistanceSlider.Value = lMinDistSlider;
  565.         trackbarMaxdistanceSlider.Value = lMaxDistSlider;
  566.     }
  567.  
  568.     private void buttonSoundfile_Click(object sender, System.EventArgs e)
  569.     {
  570.         // Stop the timer while dialogs are displayed
  571.         timerMovement.Enabled = false;
  572.         if (OpenSoundFile())
  573.             timerMovement.Enabled = true;
  574.     }
  575.     bool OpenSoundFile() 
  576.     {
  577.         //-----------------------------------------------------------------------------
  578.         // Name: OnOpenSoundFile()
  579.         // Desc: Called when the user requests to open a sound file
  580.         //-----------------------------------------------------------------------------
  581.         
  582.         OpenFileDialog ofd = new OpenFileDialog();
  583.         Guid guid3DAlgorithm = Guid.Empty;
  584.         
  585.         // Get the default media path (something like C:\WINDOWS\MEDIA)
  586.         if (string.Empty == Path)
  587.             Path = Environment.SystemDirectory.Substring(0, Environment.SystemDirectory.LastIndexOf("\\")) + "\\media";
  588.  
  589.         ofd.DefaultExt = ".wav";
  590.         ofd.Filter = "Wave Files|*.wav|All Files|*.*";
  591.         ofd.FileName = FileName;
  592.         ofd.InitialDirectory = Path;
  593.  
  594.         if (null != applicationBuffer)
  595.         {
  596.             applicationBuffer.Stop();
  597.             applicationBuffer.SetCurrentPosition(0);
  598.         }
  599.  
  600.         // Update the UI controls to show the sound as loading a file
  601.         buttonPlay.Enabled = buttonStop.Enabled = false;
  602.         labelStatus.Text = "Loading file...";
  603.  
  604.         // Display the OpenFileName dialog. Then, try to load the specified file
  605.         if (DialogResult.Cancel == ofd.ShowDialog(this))
  606.         {
  607.             labelStatus.Text = "Load aborted.";
  608.             timerMovement.Enabled = true;
  609.             return false;
  610.         }
  611.         FileName = ofd.FileName;        
  612.         Path =  ofd.FileName.Substring(0, ofd.FileName.LastIndexOf("\\"));
  613.  
  614.         // Free any previous sound, and make a new one
  615.         if (null != applicationBuffer)
  616.         {
  617.             applicationBuffer.Dispose();
  618.             applicationBuffer = null;
  619.         }
  620.         
  621.         // Get the software DirectSound3D emulation algorithm to use
  622.         // Ask the user for this sample, so display the algorithm dialog box.       
  623.         AlgorithmForm frm = new AlgorithmForm();
  624.         if (DialogResult.Cancel == frm.ShowDialog(this))
  625.         {
  626.             // User canceled dialog box
  627.             labelStatus.Text = "Load aborted.";
  628.             labelFilename.Text = string.Empty;
  629.             return false;
  630.         }
  631.  
  632.         LoadSoundFile(ofd.FileName);
  633.         if (null == applicationBuffer)
  634.             return false;
  635.  
  636.         // Get the 3D buffer from the secondary buffer
  637.         try
  638.         {
  639.             applicationBuffer3D = new Buffer3D(applicationBuffer);
  640.         }
  641.         catch(DirectXException)
  642.         {
  643.             labelStatus.Text = "Could not get 3D buffer.";
  644.             labelFilename.Text = string.Empty;
  645.             return false;
  646.         }
  647.         
  648.         // Get the 3D buffer parameters
  649.         application3DSettings = applicationBuffer3D.AllParameters;
  650.  
  651.         // Set new 3D buffer parameters
  652.         application3DSettings.Mode = Mode3D.HeadRelative;
  653.         applicationBuffer3D.AllParameters = application3DSettings;
  654.  
  655.         if (true == applicationBuffer.Caps.LocateInHardware)
  656.             labelStatus.Text = "File loaded using hardware mixing.";
  657.         else
  658.             labelStatus.Text = "File loaded using software mixing.";
  659.  
  660.         // Update the UI controls to show the sound as the file is loaded
  661.         labelFilename.Text = FileName;
  662.         EnablePlayUI(true);
  663.  
  664.         // Remember the file for next time
  665.         if (null != applicationBuffer3D)
  666.         {
  667.             FileName = ofd.FileName;
  668.         }
  669.         Path =  FileName.Substring(0, FileName.LastIndexOf("\\"));
  670.  
  671.         // Set the slider positions
  672.         SetSlidersPos(0.0f, 0.0f, maxOrbitRadius, maxOrbitRadius * 2.0f);
  673.         SliderChanged();
  674.         return true;
  675.     }
  676.     private void LoadSoundFile(string FileName)
  677.     {
  678.         BufferDescription description = new BufferDescription();
  679.         WaveFormat wf = new WaveFormat();
  680.  
  681.         buttonPlay.Enabled = false;
  682.         buttonStop.Enabled = false;
  683.         labelStatus.Text = "Loading file...";
  684.  
  685.         description.Guid3DAlgorithm = guid3DAlgorithm;
  686.         description.Control3D = true;
  687.  
  688.         if (null != applicationBuffer)
  689.         {
  690.             applicationBuffer.Stop();
  691.             applicationBuffer.SetCurrentPosition(0);
  692.         }
  693.  
  694.         // Load the wave file into a DirectSound buffer
  695.         try
  696.         {
  697.             applicationBuffer = new SecondaryBuffer(FileName, description, applicationDevice);
  698.             if (applicationBuffer.NotVirtualized)
  699.                 MessageBox.Show(this, "The 3D virtualization algorithm requested is not supported under this " + 
  700.                     "operating system.  It is available only on Windows 2000, Windows ME, and Windows 98 with WDM " +
  701.                     "drivers and beyond. This buffer was created without virtualization.", "DirectSound Sample", MessageBoxButtons.OK);
  702.         }
  703.         catch(ArgumentException)
  704.         {
  705.             // Check to see if it was a stereo buffer that threw the exception.
  706.             labelStatus.Text = "Wave file must be mono for 3D control.";
  707.             labelFilename.Text = string.Empty;
  708.             return;
  709.         }
  710.         catch
  711.         {
  712.             // Unknown error, but not a critical failure, so just update the status
  713.             labelStatus.Text = "Could not create sound buffer.";
  714.             return;
  715.         }
  716.         
  717.         if (WaveFormatTag.Pcm != (WaveFormatTag.Pcm & description.Format.FormatTag))
  718.         {
  719.             labelStatus.Text = "Wave file must be PCM for 3D control.";
  720.             if (null != applicationBuffer)
  721.                 applicationBuffer.Dispose();
  722.             applicationBuffer = null;
  723.         }
  724.  
  725.         // Remember the file for next time
  726.         if (null != applicationBuffer)
  727.             FileName = FileName;            
  728.         
  729.         labelStatus.Text = "Ready.";
  730.         labelFilename.Text = FileName;
  731.     }
  732.  
  733.     private bool RestoreBuffer()
  734.     {
  735.         if (false == applicationBuffer.Status.BufferLost)
  736.             return false;
  737.  
  738.         while(true == applicationBuffer.Status.BufferLost)
  739.         {
  740.             applicationBuffer.Restore();
  741.             Application.DoEvents();
  742.         }
  743.         return true;
  744.     }
  745.  
  746.     private void PlaySound()
  747.     {
  748.         BufferPlayFlags flags = BufferPlayFlags.Looping;
  749.  
  750.         if (RestoreBuffer())
  751.         {
  752.             LoadSoundFile(FileName);
  753.             applicationBuffer.SetCurrentPosition(0);
  754.         }
  755.         applicationBuffer.Play(0, flags);
  756.     }
  757.  
  758.     private void buttonCancel_Click(object sender, System.EventArgs e)
  759.     {
  760.         this.Close();
  761.     }
  762.  
  763.     private void buttonPlay_Click(object sender, System.EventArgs e)
  764.     {
  765.         EnablePlayUI(false);
  766.         PlaySound();
  767.     }
  768.     private void EnablePlayUI(bool bEnable)
  769.     {
  770.         if (true == bEnable)
  771.         {
  772.             buttonPlay.Enabled = true;
  773.             buttonStop.Enabled = false;
  774.             buttonPlay.Focus();
  775.         }
  776.         else
  777.         {
  778.             buttonPlay.Enabled = false;
  779.             buttonStop.Enabled = true;
  780.             buttonStop.Focus();
  781.         }
  782.     }
  783.     void SliderChanged()
  784.     {
  785.         //-----------------------------------------------------------------------------
  786.         // Name: SliderChanged()  
  787.         // Desc: Called when the dialog's slider bars are changed by the user, or need
  788.         //       updating
  789.         //-----------------------------------------------------------------------------
  790.  
  791.         // Get the position of the sliders
  792.         float DopplerFactor = ConvertLinearSliderPosToLogScale(trackbarDopplerSlider.Value);
  793.         float RolloffFactor = ConvertLinearSliderPosToLogScale(trackbarRolloffSlider.Value);
  794.         float MinDistance = ConvertLinearSliderPosToLogScale(trackbarMindistanceSlider.Value);
  795.         float MaxDistance = ConvertLinearSliderPosToLogScale(trackbarMaxdistanceSlider.Value);
  796.  
  797.         // Set the static text boxes
  798.         labelDopplerfactor.Text = DopplerFactor.ToString("0.00");
  799.         labelRollofffactor.Text = RolloffFactor.ToString("0.00");
  800.         labelMindistance.Text = MinDistance.ToString("0.00");
  801.         labelMaxdistance.Text = MaxDistance.ToString("0.00");
  802.  
  803.         // Set the options in the DirectSound buffer
  804.         Set3DParameters(DopplerFactor, RolloffFactor, MinDistance, MaxDistance);
  805.  
  806.         buttonApply.Enabled = (true == checkboxDefer.Checked) ? true : false;
  807.     }
  808.     void Set3DParameters(float DopplerFactor, float RolloffFactor, float MinDistance, float MaxDistance)
  809.     {
  810.         //-----------------------------------------------------------------------------
  811.         // Name: Set3DParameters()
  812.         // Desc: Set the 3D buffer parameters
  813.         //-----------------------------------------------------------------------------
  814.  
  815.         // Every change to 3-D sound buffer and listener settings causes 
  816.         // DirectSound to remix, at the expense of CPU cycles. 
  817.         // To minimize the performance impact of changing 3-D settings, 
  818.         // use the DS3D_DEFERRED flag in the dwApply parameter of any of 
  819.         // the IDirectSound3DListener or IDirectSound3DBuffer methods that 
  820.         // change 3-D settings. Then call the IDirectSound3DListener::CommitDeferredSettings 
  821.         // method to execute all of the deferred commands at once.
  822.  
  823.         listenerParameters.DopplerFactor = DopplerFactor;
  824.         listenerParameters.RolloffFactor = RolloffFactor;
  825.  
  826.         if (null != applicationListener)
  827.             applicationListener.AllParameters = listenerParameters;
  828.  
  829.         application3DSettings.MinDistance = MinDistance;
  830.         application3DSettings.MaxDistance = MaxDistance;
  831.     
  832.         if (null != applicationBuffer3D)
  833.             applicationBuffer3D.AllParameters  = application3DSettings;
  834.     }
  835.     void Stop()
  836.     {
  837.         if (null != applicationBuffer3D)
  838.         {
  839.             applicationBuffer.Stop();
  840.             applicationBuffer.SetCurrentPosition(0);
  841.         }
  842.  
  843.         // Update the UI controls to show the sound as stopped
  844.         EnablePlayUI(true);
  845.         labelStatus.Text = "Sound stopped.";
  846.     }
  847.     private void buttonStop_Click(object sender, System.EventArgs e)
  848.     {
  849.         EnablePlayUI(true);
  850.         Stop();
  851.     }
  852.     private void checkboxDefer_CheckedChanged(object sender, System.EventArgs e)
  853.     {
  854.         applicationBuffer3D.Deferred = checkboxDefer.Checked;
  855.         SliderChanged();
  856.     }
  857.     private void Apply()
  858.     {
  859.         // Call the DirectSound.3DListener.CommitDeferredSettings 
  860.         // method to execute all of the deferred commands at once.
  861.         // This is many times more efficent than recomputing everything
  862.         // for every call.
  863.         if (null != applicationListener)
  864.             applicationListener.CommitDeferredSettings();
  865.     }
  866.     private void buttonApply_Click(object sender, System.EventArgs e)
  867.     {       
  868.         Apply();
  869.     }
  870.     private void MovementTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  871.     {
  872.         //-----------------------------------------------------------------------------
  873.         // Name: MovementTimer_Elapsed()
  874.         // Desc: Periodically updates the position of the object 
  875.         //-----------------------------------------------------------------------------
  876.  
  877.         float XScale;
  878.         float YScale;
  879.  
  880.         XScale = trackbarHorizontalSlider.Value  / 110.0f;
  881.         YScale = trackbarVerticalSlider.Value / 110.0f;
  882.         float t = Environment.TickCount /1000.0f;
  883.  
  884.         // Move the sound object around the listener. The maximum radius of the
  885.         // orbit is 27.5 units.
  886.         Vector3 vPosition;
  887.         vPosition.X = maxOrbitRadius * XScale * (float)Math.Sin(t);
  888.         vPosition.Y = 0.0f;
  889.         vPosition.Z = maxOrbitRadius * YScale * (float)Math.Cos(t);
  890.  
  891.         Vector3 vVelocity;
  892.         vVelocity.X = maxOrbitRadius * XScale * (float)Math.Sin(t+0.05f);
  893.         vVelocity.Y = 0.0f;
  894.         vVelocity.Z = maxOrbitRadius * YScale * (float)Math.Cos(t+0.05f);
  895.  
  896.         // Show the object's position on the dialog's grid control
  897.         UpdateGrid(vPosition.X, vPosition.Z);
  898.  
  899.         // Set the sound buffer velocity and position
  900.         SetObjectProperties(vPosition, vVelocity);
  901.     }
  902.  
  903.     void UpdateGrid(float x, float y)
  904.     {
  905.         //-----------------------------------------------------------------------------
  906.         // Name: UpdateGrid()
  907.         // Desc: Draws a red dot in the dialog's grid bitmap at the x,y coordinate.
  908.         //-----------------------------------------------------------------------------         
  909.  
  910.         if (null == bitmapGrid)
  911.         {
  912.             bitmapGrid = new Bitmap(pictureboxRenderWindow.BackgroundImage);
  913.             applicationGraphics = pictureboxRenderWindow.CreateGraphics();
  914.         }
  915.  
  916.         // Convert the world space x,y coordinates to pixel coordinates
  917.         X = (int)((x/maxOrbitRadius + 1) * (pictureboxRenderWindow.ClientRectangle.Left + pictureboxRenderWindow.ClientRectangle.Right) / 2);
  918.         Y = (int)((-y/maxOrbitRadius + 1) * (pictureboxRenderWindow.ClientRectangle.Top + pictureboxRenderWindow.ClientRectangle.Bottom) / 2);
  919.  
  920.         // Draw a crosshair object in red pixels
  921.         Rectangle r = new Rectangle(X, Y, 3, 3);
  922.  
  923.         applicationGraphics.DrawImage(bitmapGrid, 1, 1);
  924.         applicationGraphics.DrawEllipse(new Pen(Color.Red), r);
  925.     }
  926.     void SetObjectProperties(Vector3 position, Vector3 velocity)
  927.     {
  928.         //-----------------------------------------------------------------------------
  929.         // Name: SetObjectProperties()
  930.         // Desc: Sets the position and velocity on the 3D buffer
  931.         //-----------------------------------------------------------------------------
  932.  
  933.         // Every change to 3-D sound buffer and listener settings causes 
  934.         // DirectSound to remix, at the expense of CPU cycles. 
  935.         // To minimize the performance impact of changing 3-D settings, 
  936.         // use the Deferred flag of the DirectSound.Listener3D or DirectSound.Buffer3D methods that 
  937.         // change 3-D settings. Then call the DirectSound.Listener3D.CommitSettings 
  938.         // method to execute all of the deferred commands at once.
  939.         application3DSettings.Position = position;
  940.         application3DSettings.Velocity = velocity;
  941.  
  942.         if (null != applicationBuffer3D)
  943.         {
  944.             applicationBuffer3D.AllParameters = application3DSettings;
  945.         }
  946.     }
  947.     private void trackbarSlider_Scroll(object sender, System.EventArgs e)
  948.     {
  949.         SliderChanged();    
  950.     }
  951. }